import Vue from "vue";
import message from "./src/message.vue";
const MessageBox = Vue.extend(message);

let instances = [];

const AsMessage = (options, type, duration, icon) => {
  if (options === null || options === undefined) {
    options = {
      message: ""
    };
  } else if (typeof options === "string" || typeof options === "number") {
    options = {
      message: options
    };
  }

  if (type) {
    options.type = type;
  }

  if (duration) {
    options.duration = duration;
  }

  if (icon) {
    options.icon = icon;
  }

  options.visible = false;
  options.instances = instances;
  options.top = 0;

  const instance = new MessageBox({
    data: options
  }).$mount();

  instances.push(instance);
  document.body.appendChild(instance.$el);
};

["success", "error", "warning", "info"].forEach(item => {
  AsMessage[item] = options => {
    // options.type = item;
    if (typeof options === "string" || typeof options === "number") {
      options = {
        message: options
      };
    }
    options.type = item;
    return AsMessage(options);
  };
});

export default AsMessage;
